<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ES入门 - java整合 | java知识体系</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/lm_blog_vdoing/img/favicon.ico">
    <meta name="description" content="lm_blog">
    <meta name="author" content="小猫爱吃鱼">
    <meta name="keywords" content="小猫爱吃鱼|java开发学习">
    <link rel="preload" href="/lm_blog_vdoing/assets/css/0.styles.6837ef6d.css" as="style"><link rel="preload" href="/lm_blog_vdoing/assets/js/app.fa0e5fdd.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/2.d9c93b35.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/21.058f688b.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/5.e0bc0396.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/13.ee87d1ca.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/14.9d4d9cf6.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/9.fc3a629f.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/10.4c34cde9.js" as="script"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/11.bccd7768.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/12.e841fec0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/15.ea506303.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/16.2da770e9.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/17.2e0242dc.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/18.304c9e7d.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/19.4b93faf0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/20.87daf9d0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/22.9937ce9d.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/23.87e8eccb.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/24.1688627c.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/25.04906008.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/26.494f706b.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/27.01c810da.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/28.9fbe8e29.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/29.3a1e938a.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/3.80bae7d2.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/30.4adb3ea1.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/31.5001aed0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/32.306c6420.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/33.5fcce29f.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/34.2de40b8f.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/4.d2df651a.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/6.cbc45c4c.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/7.fd589d8e.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/8.35156f8e.js">
    <link rel="stylesheet" href="/lm_blog_vdoing/assets/css/0.styles.6837ef6d.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/lm_blog_vdoing/" class="home-link router-link-active"><!----> <span class="site-name">java知识体系</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/lm_blog_vdoing/" class="nav-link">首页​</a></div><div class="nav-item"><a href="/lm_blog_vdoing/pages/f6054a/" class="nav-link">❁导航​</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机核心" class="dropdown-title"><!----> <span class="title" style="display:;">计算机核心</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/db72cf/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39e2a1/" class="nav-link">计算机网络</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><!----> <span class="title" style="display:;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e05ef5/" class="nav-link">JavaSE</a></li></ul></li><li class="dropdown-item"><h4>JavaWeb</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/5cda88/" class="nav-link">HTML</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cea341/" class="nav-link">CSS</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/d893c0/" class="nav-link">JavaScript</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8f83ba/" class="nav-link">Servlet</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8bc1c4/" class="nav-link">MVC</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/f883e2/" class="nav-link">filter|listener</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Spring生态" class="dropdown-title"><!----> <span class="title" style="display:;">Spring生态</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/462a90/" class="nav-link">Spring5</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/2e990c/" class="nav-link">SpringMVC</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/0d4af0/" class="nav-link">SpringBoot2</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/870083/" class="nav-link">SpringCloud</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/66babb/" class="nav-link">SpringSecurity</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><!----> <span class="title" style="display:;">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>消息队列</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e645d9/" class="nav-link">RabbitMQ</a></li></ul></li><li class="dropdown-item"><h4>服务器</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9551ee/" class="nav-link">Nginx🌐</a></li></ul></li><li class="dropdown-item"><h4>服务框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/48771f/" class="nav-link">Dubbo</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="大数据" class="dropdown-title"><!----> <span class="title" style="display:;">大数据</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Python</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/b9268d/" class="nav-link">Python基础</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e0bd06/" class="nav-link">数据分析</a></li></ul></li><li class="dropdown-item"><h4>环境搭建</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/600247/" class="nav-link">Hadoop集群</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cdeb68/" class="nav-link">KVM虚拟化技术</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>SQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e72480/" class="nav-link">MySQL</a></li></ul></li><li class="dropdown-item"><h4>NoSQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e9cc9f/" class="nav-link">Redis</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/91197c/" class="nav-link">MongoDB</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/7b46ed/" class="nav-link">ElasticSearch</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><!----> <span class="title" style="display:;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/99e9dc/" class="nav-link">Linux</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/90cc29/" class="nav-link">Docker</a></li></ul></li><li class="dropdown-item"><h4>管理</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/45eca1/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/34892c/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面试" class="dropdown-title"><!----> <span class="title" style="display:;">面试</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/eab19d/" class="nav-link">十大排序算法</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/40ee62/" class="nav-link">力扣算法题</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39558d/" class="nav-link">初级开发篇</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/47c622/" class="nav-link">中高进阶篇</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><!----> <span class="title" style="display:;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/myfriends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="本站" class="dropdown-title"><!----> <span class="title" style="display:;">本站</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>索引</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/categories/" class="nav-link">分类</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/tags/" class="nav-link">标签</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/archives/" class="nav-link">归档</a></li></ul></li><li class="dropdown-item"><h4>其他</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9013e4/" class="nav-link">关于</a></li></ul></li></ul></div></div> <a href="https://gitee.com/lmgghub/lm_blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    查看源码
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.staticaly.com/gh/xustudyxu/image-hosting@master/20220627/mmexport1656324824543.124zxwkqyzlc.webp"> <div class="blogger-info"><h3>lm_blog</h3> <span>持续学习!</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/lm_blog_vdoing/" class="nav-link">首页​</a></div><div class="nav-item"><a href="/lm_blog_vdoing/pages/f6054a/" class="nav-link">❁导航​</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机核心" class="dropdown-title"><!----> <span class="title" style="display:;">计算机核心</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/db72cf/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39e2a1/" class="nav-link">计算机网络</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><!----> <span class="title" style="display:;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e05ef5/" class="nav-link">JavaSE</a></li></ul></li><li class="dropdown-item"><h4>JavaWeb</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/5cda88/" class="nav-link">HTML</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cea341/" class="nav-link">CSS</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/d893c0/" class="nav-link">JavaScript</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8f83ba/" class="nav-link">Servlet</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8bc1c4/" class="nav-link">MVC</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/f883e2/" class="nav-link">filter|listener</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Spring生态" class="dropdown-title"><!----> <span class="title" style="display:;">Spring生态</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/462a90/" class="nav-link">Spring5</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/2e990c/" class="nav-link">SpringMVC</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/0d4af0/" class="nav-link">SpringBoot2</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/870083/" class="nav-link">SpringCloud</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/66babb/" class="nav-link">SpringSecurity</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><!----> <span class="title" style="display:;">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>消息队列</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e645d9/" class="nav-link">RabbitMQ</a></li></ul></li><li class="dropdown-item"><h4>服务器</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9551ee/" class="nav-link">Nginx🌐</a></li></ul></li><li class="dropdown-item"><h4>服务框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/48771f/" class="nav-link">Dubbo</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="大数据" class="dropdown-title"><!----> <span class="title" style="display:;">大数据</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Python</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/b9268d/" class="nav-link">Python基础</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e0bd06/" class="nav-link">数据分析</a></li></ul></li><li class="dropdown-item"><h4>环境搭建</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/600247/" class="nav-link">Hadoop集群</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cdeb68/" class="nav-link">KVM虚拟化技术</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>SQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e72480/" class="nav-link">MySQL</a></li></ul></li><li class="dropdown-item"><h4>NoSQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e9cc9f/" class="nav-link">Redis</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/91197c/" class="nav-link">MongoDB</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/7b46ed/" class="nav-link">ElasticSearch</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><!----> <span class="title" style="display:;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/99e9dc/" class="nav-link">Linux</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/90cc29/" class="nav-link">Docker</a></li></ul></li><li class="dropdown-item"><h4>管理</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/45eca1/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/34892c/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面试" class="dropdown-title"><!----> <span class="title" style="display:;">面试</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/eab19d/" class="nav-link">十大排序算法</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/40ee62/" class="nav-link">力扣算法题</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39558d/" class="nav-link">初级开发篇</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/47c622/" class="nav-link">中高进阶篇</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><!----> <span class="title" style="display:;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/myfriends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="本站" class="dropdown-title"><!----> <span class="title" style="display:;">本站</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>索引</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/categories/" class="nav-link">分类</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/tags/" class="nav-link">标签</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/archives/" class="nav-link">归档</a></li></ul></li><li class="dropdown-item"><h4>其他</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9013e4/" class="nav-link">关于</a></li></ul></li></ul></div></div> <a href="https://gitee.com/lmgghub/lm_blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    查看源码
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 基础</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 进阶</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 运维部署</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>NoSQL - Redis</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>NoSQL - MongoDB</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>NoSQL - ElasticSearch</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/lm_blog_vdoing/pages/7b46ed/" class="sidebar-link">ElasticeSarch的概述</a></li><li><a href="/lm_blog_vdoing/pages/596552/" class="sidebar-link">ES入门 - 环境安装</a></li><li><a href="/lm_blog_vdoing/pages/bc6018/" class="sidebar-link">ES入门 - 基本使用：索引和文档</a></li><li><a href="/lm_blog_vdoing/pages/6e6e0c/" class="sidebar-link">ES入门 - 分片和副本</a></li><li><a href="/lm_blog_vdoing/pages/3d97b1/" class="sidebar-link">ES入门 - 分析器</a></li><li><a href="/lm_blog_vdoing/pages/b54e3a/" aria-current="page" class="active sidebar-link">ES入门 - java整合</a></li><li><a href="/lm_blog_vdoing/pages/7e4dff/" class="sidebar-link">ES进阶 - 分片操作原理</a></li><li><a href="/lm_blog_vdoing/pages/b82771/" class="sidebar-link">ES进阶 - 并发问题</a></li><li><a href="/lm_blog_vdoing/pages/c3becc/" class="sidebar-link">ES进阶 - 集群搭建</a></li><li><a href="/lm_blog_vdoing/pages/6d9622/" class="sidebar-link">ES进阶 - 优化配置</a></li><li><a href="/lm_blog_vdoing/pages/da009f/" class="sidebar-link">ES进阶 - 框架整合</a></li><li><a href="/lm_blog_vdoing/pages/7cb809/" class="sidebar-link">ElasticSearch面试题</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-1baff76c><div class="articleInfo" data-v-1baff76c><ul class="breadcrumbs" data-v-1baff76c><li data-v-1baff76c><a href="/lm_blog_vdoing/" title="首页" class="iconfont icon-home router-link-active" data-v-1baff76c></a></li> <li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=notes" title="分类" data-v-1baff76c>notes</a></li><li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=database" title="分类" data-v-1baff76c>database</a></li><li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=elasticSearch" title="分类" data-v-1baff76c>elasticSearch</a></li></ul> <div class="info" data-v-1baff76c><div title="作者" class="author iconfont icon-touxiang" data-v-1baff76c><a href="javascript:;" data-v-1baff76c>lm</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-1baff76c><a href="javascript:;" data-v-1baff76c>2023-02-16</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">ES入门 - java整合<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="es入门-java整合"><a href="#es入门-java整合" class="header-anchor">#</a> ES入门 - java整合</h1> <h2 id="环境准备"><a href="#环境准备" class="header-anchor">#</a> 环境准备</h2> <blockquote><p>需要本机安装jdk、开发工具idea</p></blockquote> <ul><li>创建一个maven项目</li> <li>导入ES jar包坐标文件</li></ul> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- elasticsearch 的客户端 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.elasticsearch.client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>elasticsearch-rest-high-level-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>7.8.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- elasticsearch 依赖 2.x 的 log4j --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>log4j-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.apache.logging.log4j<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>log4j-core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.8.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.fasterxml.jackson.core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>jackson-databind<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.9.3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- junit 单元测试 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>junit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="索引操作"><a href="#索引操作" class="header-anchor">#</a> 索引操作</h2> <h3 id="创建索引"><a href="#创建索引" class="header-anchor">#</a> 创建索引</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//创建ES客户端</span>
<span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">CreateIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CreateIndexRequest</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">CreateIndexResponse</span> createIndexResponse <span class="token operator">=</span>
    esClient<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//创建索引响应状态</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>createIndexResponse<span class="token punctuation">.</span><span class="token function">isAcknowledged</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>
<span class="token boolean">true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="查看索引"><a href="#查看索引" class="header-anchor">#</a> 查看索引</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">GetIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">GetIndexRequest</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">GetIndexResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取查询到索引信息</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getMappings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getSettings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getAliases</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">{</span>car<span class="token operator">=</span><span class="token class-name"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>metadata<span class="token punctuation">.</span></span>MappingMetadata</span><span class="token annotation punctuation">@b24cc87e</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span>car<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;index.creation_date&quot;</span><span class="token operator">:</span><span class="token string">&quot;1676539982762&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index.number_of_replicas&quot;</span><span class="token operator">:</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index.number_of_shards&quot;</span><span class="token operator">:</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index.provided_name&quot;</span><span class="token operator">:</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index.uuid&quot;</span><span class="token operator">:</span><span class="token string">&quot;KH-yRnMKStKOA2sTLvKztQ&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;index.version.created&quot;</span><span class="token operator">:</span><span class="token string">&quot;7080099&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span>car<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="删除索引"><a href="#删除索引" class="header-anchor">#</a> 删除索引</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DeleteIndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DeleteIndexRequest</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">AcknowledgedResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//获取删除状态</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">isAcknowledged</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token boolean">true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="文档操作"><a href="#文档操作" class="header-anchor">#</a> 文档操作</h2> <h3 id="新增文档"><a href="#新增文档" class="header-anchor">#</a> 新增文档</h3> <ul><li>单个新增</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
     <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
 <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//插入数据</span>
<span class="token class-name">IndexRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Car</span> user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Car</span><span class="token punctuation">(</span><span class="token string">&quot;奔驰&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;c260&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//向ES插入数据，必须将数据转换为JSON格式</span>
<span class="token class-name">ObjectMapper</span> mapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> userJson <span class="token operator">=</span> mapper<span class="token punctuation">.</span><span class="token function">writeValueAsString</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>userJson<span class="token punctuation">,</span> <span class="token class-name">XContentType</span><span class="token punctuation">.</span>JSON<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">IndexResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>CREATED
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>批量新增</li></ul> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="方法一" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建数据对象</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span>JSON<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span>JSON<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span>JSON<span class="token punctuation">,</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;300&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 客户端发送请求，获取响应对象</span>
<span class="token class-name">BulkResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;响应时间：&quot;</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;创建的内容：&quot;</span> <span class="token operator">+</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></section> <section aria-hidden="true" id="方法二" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 通过Map(String,Object) 构建source中的参数</span>
<span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkRequest</span> request2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> jsonMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
jsonMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
jsonMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
jsonMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;modelName&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;星悦&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request2<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>jsonMap<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request2<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;响应时间：&quot;</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;创建的内容：&quot;</span> <span class="token operator">+</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></section> <section aria-hidden="true" id="方法三" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token class-name">XContentBuilder</span> builder <span class="token operator">=</span> <span class="token class-name">XContentFactory</span><span class="token punctuation">.</span><span class="token function">jsonBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
builder<span class="token punctuation">.</span><span class="token function">startObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">{</span>
    builder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;小红&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    builder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    builder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;sex&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;女&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
builder<span class="token punctuation">.</span><span class="token function">endObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkRequest</span> request3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request3<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">IndexRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;student&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;1007&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>builder<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request3<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;响应时间：&quot;</span> <span class="token operator">+</span> response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;创建的内容：&quot;</span> <span class="token operator">+</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getItems</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></section></div></div> <ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间：<span class="token number">9</span>ms
创建的内容：<span class="token punctuation">[</span><span class="token class-name"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span>BulkItemResponse</span><span class="token annotation punctuation">@38102d01</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span>BulkItemResponse</span><span class="token annotation punctuation">@37883b97</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token namespace">org<span class="token punctuation">.</span>elasticsearch<span class="token punctuation">.</span>action<span class="token punctuation">.</span>bulk<span class="token punctuation">.</span></span>BulkItemResponse</span><span class="token annotation punctuation">@6ab778a</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="查看文档"><a href="#查看文档" class="header-anchor">#</a> 查看文档</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="查看单个文档" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
<span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//查询数据</span>
<span class="token class-name">GetRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">GetRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">GetResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></section> <section aria-hidden="true" id="查看全部文档" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
<span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//查询所有索引数据</span>
<span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchSourceBuilder</span> query <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>query<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;总共的查询条数:&quot;</span><span class="token operator">+</span>hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;查询时间:&quot;</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;详细数据；&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// 输出每条查询的结果信息</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div></section></div></div> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="查看单个文档返回结果" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="查看全部文档返回结果" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code>总共的查询条数<span class="token operator">:</span><span class="token number">5</span> hits
查询时间<span class="token operator">:</span><span class="token number">4</span>ms
详细数据；
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奔驰&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;c260&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;300&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星悦&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></section></div></div> <h3 id="修改文档"><a href="#修改文档" class="header-anchor">#</a> 修改文档</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//修改数据</span>
<span class="token class-name">UpdateRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UpdateRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">doc</span><span class="token punctuation">(</span><span class="token class-name">XContentType</span><span class="token punctuation">.</span>JSON<span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">UpdateResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li>返回结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>UPDATED
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="删除文档"><a href="#删除文档" class="header-anchor">#</a> 删除文档</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="单个删除" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
<span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DeleteRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">DeleteResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></section> <section aria-hidden="true" id="批量删除" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">BulkRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BulkRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//配置修改参数</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;4&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DeleteRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string">&quot;5&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//客户端发送请求，获取响应对象</span>
<span class="token class-name">BulkResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">bulk</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></section></div></div> <ul><li>返回信息</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>DELETED
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="高级查询"><a href="#高级查询" class="header-anchor">#</a> 高级查询</h3> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token comment">//先插入5组数据用于测试</span>
<span class="token punctuation">{</span><span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token property">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token property">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token property">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;300&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token property">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token property">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token property">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="条件查询-term"><a href="#条件查询-term" class="header-anchor">#</a> 条件查询 - term</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RestHighLevelClient</span> esClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RestHighLevelClient</span><span class="token punctuation">(</span>
    <span class="token class-name">RestClient</span><span class="token punctuation">.</span><span class="token function">builder</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">HttpHost</span><span class="token punctuation">(</span><span class="token string">&quot;localhost&quot;</span><span class="token punctuation">,</span><span class="token number">9200</span><span class="token punctuation">,</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchRequest</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">indices</span><span class="token punctuation">(</span><span class="token string">&quot;car&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//构建查询的请求体</span>
<span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termQuery</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchHits</span> hits <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;响应时间:&quot;</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">getTook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;是否超时:&quot;</span><span class="token operator">+</span>response<span class="token punctuation">.</span><span class="token function">isTimedOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;命中数量:&quot;</span><span class="token operator">+</span>hits<span class="token punctuation">.</span><span class="token function">getTotalHits</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;MaxScore:&quot;</span><span class="token operator">+</span>hits<span class="token punctuation">.</span><span class="token function">getMaxScore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;详细数据:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span> hit <span class="token operator">:</span> hits<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>hit<span class="token punctuation">.</span><span class="token function">getSourceAsString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
esClient<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">2</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">2</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.0</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="条件查询-terms"><a href="#条件查询-terms" class="header-anchor">#</a> 条件查询 - terms</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//除请求体部分其他与上面保持一致</span>
<span class="token comment">//构建查询的请求体 类型mysql in</span>
<span class="token class-name">SearchSourceBuilder</span> sourceBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SearchSourceBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">termsQuery</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">SearchResponse</span> response <span class="token operator">=</span> esClient<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span><span class="token function">source</span><span class="token punctuation">(</span>sourceBuilder<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">RequestOptions</span><span class="token punctuation">.</span>DEFAULT<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">14</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">4</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.0</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="分页查询"><a href="#分页查询" class="header-anchor">#</a> 分页查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code>sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">5</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">5</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.0</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;300&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="排序查询"><a href="#排序查询" class="header-anchor">#</a> 排序查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code>sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> <span class="token class-name">SortOrder</span><span class="token punctuation">.</span>DESC<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">5</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">5</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token class-name">NaN</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;300&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="过滤字段"><a href="#过滤字段" class="header-anchor">#</a> 过滤字段</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//过滤字段信息 excludes排除  includes包括</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchAllQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> excludes<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> includes<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">fetchSource</span><span class="token punctuation">(</span>includes<span class="token punctuation">,</span>excludes<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">4</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">5</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.0</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;坦克&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">3</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="bool查询"><a href="#bool查询" class="header-anchor">#</a> Bool查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">BoolQueryBuilder</span> boolQueryBuilder <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">boolQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//必须包含  类似and</span>
boolQueryBuilder<span class="token punctuation">.</span><span class="token function">must</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//一定不含  类似not</span>
boolQueryBuilder<span class="token punctuation">.</span><span class="token function">mustNot</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//可能包含  类型or</span>
boolQueryBuilder<span class="token punctuation">.</span><span class="token function">should</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;modelName&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>boolQueryBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">6</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">1</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.744874</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="范围查询"><a href="#范围查询" class="header-anchor">#</a> 范围查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">RangeQueryBuilder</span> rangeQuery <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">rangeQuery</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
rangeQuery<span class="token punctuation">.</span><span class="token function">gte</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
rangeQuery<span class="token punctuation">.</span><span class="token function">lte</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>rangeQuery<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">1</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">4</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.0</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;宝马&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;x1&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;奥迪&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;A4&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="模糊查询"><a href="#模糊查询" class="header-anchor">#</a> 模糊查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">FuzzyQueryBuilder</span> fuzziness <span class="token operator">=</span> <span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">fuzzyQuery</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;吉&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fuzziness</span><span class="token punctuation">(</span><span class="token class-name">Fuzziness</span><span class="token punctuation">.</span>ZERO<span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>fuzziness<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">5</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">2</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">0.87546873</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="高亮查询"><a href="#高亮查询" class="header-anchor">#</a> 高亮查询</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code>sourceBuilder<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">matchQuery</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">HighlightBuilder</span> highlightBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HighlightBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
highlightBuilder<span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">preTags</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;font color='red'&gt;&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">postTags</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;/font&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
sourceBuilder<span class="token punctuation">.</span><span class="token function">highlighter</span><span class="token punctuation">(</span>highlightBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>结果</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code>响应时间<span class="token operator">:</span><span class="token number">1</span>ms
是否超时<span class="token operator">:</span><span class="token boolean">false</span>
命中数量<span class="token operator">:</span><span class="token number">2</span> hits
<span class="token class-name">MaxScore</span><span class="token operator">:</span><span class="token number">1.7509375</span>
详细数据<span class="token operator">:</span>
<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">,</span> fragments<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">&lt;</span>font color<span class="token operator">=</span><span class="token char">'red'</span><span class="token operator">&gt;</span>吉<span class="token operator">&lt;</span><span class="token operator">/</span>font<span class="token operator">&gt;</span><span class="token operator">&lt;</span>font color<span class="token operator">=</span><span class="token char">'red'</span><span class="token operator">&gt;</span>利<span class="token operator">&lt;</span><span class="token operator">/</span>font<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星越&quot;</span><span class="token punctuation">}</span>
<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">,</span> fragments<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">&lt;</span>font color<span class="token operator">=</span><span class="token char">'red'</span><span class="token operator">&gt;</span>吉<span class="token operator">&lt;</span><span class="token operator">/</span>font<span class="token operator">&gt;</span><span class="token operator">&lt;</span>font color<span class="token operator">=</span><span class="token char">'red'</span><span class="token operator">&gt;</span>利<span class="token operator">&lt;</span><span class="token operator">/</span>font<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;吉利&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;age&quot;</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;modelName&quot;</span><span class="token operator">:</span><span class="token string">&quot;星瑞&quot;</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="聚合查询"><a href="#聚合查询" class="header-anchor">#</a> 聚合查询</h4> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="最大值" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code> sourceBuilder<span class="token punctuation">.</span><span class="token function">aggregation</span><span class="token punctuation">(</span><span class="token class-name">AggregationBuilders</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token string">&quot;maxAge&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="分组" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-java line-numbers-mode"><pre class="language-java"><code> sourceBuilder<span class="token punctuation">.</span><span class="token function">aggregation</span><span class="token punctuation">(</span><span class="token class-name">AggregationBuilders</span><span class="token punctuation">.</span><span class="token function">terms</span><span class="token punctuation">(</span><span class="token string">&quot;ageGroupby&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <p>这里还有很多函数,就不一一例举了!</p></div></div></div></div> <!----> <div class="page-edit"><!----> <div class="tags"><a href="/lm_blog_vdoing/tags/?tag=ElasticSearch" title="标签">#ElasticSearch</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/lm_blog_vdoing/pages/3d97b1/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">ES入门 - 分析器</div></a> <a href="/lm_blog_vdoing/pages/7e4dff/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">ES进阶 - 分片操作原理</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/lm_blog_vdoing/pages/3d97b1/" class="prev">ES入门 - 分析器</a></span> <span class="next"><a href="/lm_blog_vdoing/pages/7e4dff/">ES进阶 - 分片操作原理</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/lm_blog_vdoing/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/lm_blog_vdoing/pages/6d9622/"><div>
            ES进阶 - 优化配置
            <!----></div></a> <span class="date">02-20</span></dt></dl><dl><dd>02</dd> <dt><a href="/lm_blog_vdoing/pages/7e4dff/"><div>
            ES进阶 - 分片操作原理
            <!----></div></a> <span class="date">02-20</span></dt></dl><dl><dd>03</dd> <dt><a href="/lm_blog_vdoing/pages/b82771/"><div>
            ES进阶 - 并发问题
            <!----></div></a> <span class="date">02-20</span></dt></dl> <dl><dd></dd> <dt><a href="/lm_blog_vdoing/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:1512375133@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://gitee.com/lmgghub/" title="gitee" target="_blank" class="iconfont icon-gitee"></a><a href="https://juejin.cn/user/2295436010326072" title="掘金" target="_blank" class="iconfont icon-juejin"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2023
    <span>lm </span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div><div></div><div></div><div id="tcomment"></div><!----></div></div>
    <script src="/lm_blog_vdoing/assets/js/app.fa0e5fdd.js" defer></script><script src="/lm_blog_vdoing/assets/js/2.d9c93b35.js" defer></script><script src="/lm_blog_vdoing/assets/js/21.058f688b.js" defer></script><script src="/lm_blog_vdoing/assets/js/5.e0bc0396.js" defer></script><script src="/lm_blog_vdoing/assets/js/13.ee87d1ca.js" defer></script><script src="/lm_blog_vdoing/assets/js/14.9d4d9cf6.js" defer></script><script src="/lm_blog_vdoing/assets/js/9.fc3a629f.js" defer></script><script src="/lm_blog_vdoing/assets/js/10.4c34cde9.js" defer></script>
  </body>
</html>
